package servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.Order;
import dao.OrderDao;
import dao.OrderUtil;

/**
 * Servlet implementation class DoSaveOrder
 */
@WebServlet(
		name="DoSaveOrder",
		urlPatterns= {"/servlet/DoSaveOrder"},
		loadOnStartup = 1
		
		)
public class DoSaveOrder extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DoSaveOrder() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//doGet(request, response);
		response.setContentType("text/html,charset=utf-8");
		request.setCharacterEncoding("utf-8");
		int id=Integer.parseInt(request.getParameter("id"));
		String order_id=request.getParameter("username");
		double total=Double.parseDouble(request.getParameter("total"));
		int quantity=Integer.parseInt(request.getParameter("quantity"));
		String phone=request.getParameter("phone");
		String address=request.getParameter("address");
		Order orderbean=new Order();
		orderbean.setId(id);
		orderbean.setOrder_id(order_id);
		orderbean.setTotal(total);
		orderbean.setQuantity(quantity);
		orderbean.setPhone(phone);
		orderbean.setAddress(address);
		OrderDao orderdao=new OrderUtil();
		int n=orderdao.add(orderbean);
		if(n>0) {
			request.getRequestDispatcher("/goodchange.jsp").forward(request, response);
		}else {
			request.getRequestDispatcher("/error.jsp").forward(request, response);
		}
	}

}
